using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BO.Admin;
using BO;

namespace Admin.Controls.News
{
    public partial class EditNews : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ltrTextBox.Text = txtNewsPhoto.ClientID;
            ltrWindow.Text = ExplorerWindow1.ClientID;
            if (!IsPostBack)
            {
                //BindControls();
                BindData();
            }
        }

        private void BindControls()
        {
            ddlCategory.DataSource = null;
            ddlCategory.DataSource = Category_Helper.GetAll();
            ddlCategory.DataTextField = "Category_Name";
            ddlCategory.DataValueField = "Category_ID";
            ddlCategory.DataBind();
        }

        private void BindData()
        {
            int newsid = Utils.QueryString.GetNewsID();
            if (newsid > 0)
            {
                DataTable dt = News_Helper.GetByNewsID(newsid);
                txtCostume.Text = dt.Rows[0]["Costume"] != DBNull.Value ? dt.Rows[0]["Costume"].ToString() : String.Empty;
                txtLocation.Text = dt.Rows[0]["Location"] != DBNull.Value ? dt.Rows[0]["Location"].ToString() : String.Empty;
                txtMakeup.Text = dt.Rows[0]["Makeup"] != DBNull.Value ? dt.Rows[0]["Makeup"].ToString() : String.Empty;
                txtModel.Text = dt.Rows[0]["Model"] != DBNull.Value ? dt.Rows[0]["Model"].ToString() : String.Empty;
                txtNewsTitle.Text = dt.Rows[0]["News_Title"] != DBNull.Value ? dt.Rows[0]["News_Title"].ToString() : String.Empty;
                ltrPostName.Text = txtNewsTitle.Text;
                txtPhoto.Text = dt.Rows[0]["Photo"] != DBNull.Value ? dt.Rows[0]["Photo"].ToString() : String.Empty;
                txtStylist.Text = dt.Rows[0]["Stylist"] != DBNull.Value ? dt.Rows[0]["Stylist"].ToString() : String.Empty;
                txtNewsPhoto.Text = dt.Rows[0]["News_Photo"] != DBNull.Value ? dt.Rows[0]["News_Photo"].ToString() : String.Empty;
                try
                {
                    ddlCategory.SelectedIndex = ddlCategory.Items.IndexOf(ddlCategory.Items.FindByValue(dt.Rows[0]["Category_ID"].ToString()));
                }
                catch { }
                try
                {
                    ddlStatus.SelectedIndex = ddlStatus.Items.IndexOf(ddlStatus.Items.FindByValue(dt.Rows[0]["News_Status"].ToString()));
                }
                catch { }
            }
        }

        protected void btSave_Clicked(object sender, EventArgs e)
        {
            News_Helper.Update(Utils.QueryString.GetNewsID(), Convert.ToInt32(ddlCategory.SelectedValue), txtNewsTitle.Text, txtPhoto.Text, txtStylist.Text, txtMakeup.Text,
                txtModel.Text, txtCostume.Text, txtLocation.Text, Convert.ToInt32(ddlStatus.SelectedValue), txtNewsPhoto.Text);
            BindData();
        }

        protected void btDelete_Clicked(object sender, EventArgs e)
        {
            Response.Redirect("/DeleteNews.aspx?news_ID=" + Utils.QueryString.GetNewsID());
        }
    }
}